
<template>
  <div class="accountList">
    <div class="searchBox">
      <el-form :model="searchForm" ref="searchFormRef">
        <el-row>
          <el-form-item prop="sensitiveWordName">
            <div class="item">
              <span v-html="'敏感词'" class="nameSpan"></span>
              <el-input
                size="mini"
                v-model="searchForm.sensitiveWordName"
                placeholder="请输入敏感词"
              ></el-input>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="searchClick"
              icon="el-icon-search"
              type="primary"
              size="mini"
              >查询</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetClick"
              >重置</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="topBtn">
      <el-button
        size="mini"
        @click="addClick"
        type="success"
        plain
        icon="el-icon-plus"
        v-has="'community:sensitive:add'"
        >新增敏感词</el-button
      >
    </div>
    <div class="tableBox" ref="tableBoxRef">
      <el-table
        v-loading="tableLoading"
        :header-cell-style="{
          background: '#F8F8F8',
          color: '#333',
          fontSize: '16px',
          fontWeight: '700',
        }"
        :height="screenHeight - 400"
        border
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="sensitiveWordName"
          align="center"
          label="敏感词"
           
        >
        </el-table-column>

        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit-outline"
              size="mini"
              type="text"
              v-has="'community:sensitive:edit'"
              @click="editClick(scope.row)"
              >编辑</el-button
            >
            <el-button
              icon="el-icon-delete"
              size="mini"
              type="text"
              v-has="'community:sensitive:delete'"
              @click="deleteClick(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 新增/修改权限 -->
    <el-dialog :close-on-click-modal="false" 
      :title="title"
      :visible.sync="addEditDialogVisible"
      width="400px"
      @close="addEditDialogOpen"
      class="addEditAccountDialog"
    >
      <div class="content">
        <el-form
          :model="addEditForm"
          :rules="addEditRules"
          ref="addEditFormRef"
        >
          <el-form-item prop="sensitiveWordName" label="敏感词：">
            <div class="item">
              <el-input
                show-word-limit
                type="textarea"
               maxlength="128"
                size="mini"
                v-model="addEditForm.sensitiveWordName"
                placeholder="如需添加多个，请用英文逗号隔开，最后一个词后面不可加标点符号。如：a,b,c,d"
              >
              </el-input>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <div class="btns">
        <el-button size="mini" @click="addEditDialogVisible = false"
          >取 消</el-button
        >
        <el-button
          v-clickForbidden
          size="mini"
          type="primary"
          @click="addEditDialogSaveClick"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      postHead: { token: localStorage.token },
      title: "",
      screenHeight: document.documentElement.clientHeight,
      searchForm: {
        sensitiveWordName: "",
      },
      tableLoading: false,
      tableData: [],
      addEditDialogVisible: false,
      addEditForm: {
        sensitiveWordName: "",
      },
      addEditRules: {
        sensitiveWordName: [
          { required: true, message: "敏感词不能为空", trigger: "blur" },
        ],
      },
      editId: "",
    };
  },
  created() {
    this.getSentiveList();
  },
  methods: {
            /**
     * 删除按钮
     * @param {*} row
     */
    deleteClick(row) {
      this.$confirm("确定删除当前敏感词?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
            console.log(row.id);
          this.$api.delSensitive(row.id).then((res) => {
            if (res.data.code == "00000") {
              this.$message.success("删除成功");
              this.getSentiveList();
            }
          });
        })
        .catch(() => {});
    },
    auditClick(row) {
      this.$router.push({
        path: "/operationManagement/industayView",
        query: { id: row.id, title: "审核" },
      });
    },
    getView(row) {
      this.$router.push({
        path: "/operationManagement/industayView",
        query: { id: row.id, title: "预览" },
      });
    },

    /**
     * 新增敏感词
     * @param {*}
     */
    addClick() {
      this.addEditForm={}
      this.title = "新增敏感词";
      this.addEditDialogVisible = true;
    },
    searchClick() {
      this.getSentiveList();
    },
    resetClick() {
      this.$refs.searchFormRef.resetFields();
      this.getSentiveList();
    },

    /**
     * 获取用户列表
     * @param {*}
     */
    async getSentiveList() {
      this.tableLoading = true;
      await this.$api
        .sensitiveList({
          ...this.searchForm,
        })
        .then((res) => {
          if (res.data.code == "00000") {
            this.tableData = res.data.result;
            this.tableLoading = false;
          }
        });
    },
    /**
     * 编辑按钮
     * @param {*} row
     */
    async editClick(row) {
      this.title = "编辑敏感词";
      this.addEditDialogVisible = true;
      this.editId = row.id;
      this.addEditForm = JSON.parse(JSON.stringify(row));
    },

    /**
     * 打开新增和修改的回调
     * @param {*}
     */
    addEditDialogOpen() {

   
        this.$refs.addEditFormRef.resetFields();
      
    },

    /**
     * 编辑保存按钮
     * @param {*}
     */
    addEditDialogSaveClick() {
      this.$refs.addEditFormRef.validate(async (boolean) => {
        if (boolean) {
          let data = {
            id: this.editId,
            ...this.addEditForm,
          };
          if (this.title == "新增敏感词") {
            data.id = "";
            await this.$api.addSensitive(data).then((res) => {
              if (res.data.code == "00000") {
                this.$message.success("新增成功");
                this.getSentiveList();
                this.addEditDialogVisible = false;
              }
            });
          } else {
            await this.$api.editSensitive(data).then((res) => {
              if (res.data.code == "00000") {
                this.$message.success("修改成功");
                this.getSentiveList();
                this.addEditDialogVisible = false;
              }
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.accountList {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
  .searchBox {
    .el-row {
      display: flex;
      flex-wrap: wrap;
      .el-form-item {
        margin-bottom: 0;
      }
      .item {
        display: flex;
        align-items: center;
        margin-right: 20px;
        .nameSpan {
          padding-right: 10px;
          box-sizing: border-box;
          white-space: nowrap;
        }
        .el-input {
          width: 162px;
        }
      }
    }
  }
   .topBtn {
    margin: 10px 0;
text-align: right;
  }
  .tableBox {
    flex: 1;
    .bottomBtn {
      height: 50px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }
  }
  .addEditAccountDialog {
    .el-form-item {
      .el-select {
        width: 100%;
      }
    }
    .btns {
      text-align: right;
      margin-bottom: 0;
    }
  }
}
/deep/.el-button--success.is-plain {
  color: #fff;
  border-color: #409eff;
  background-color: #409eff;
}
/deep/.el-form-item {
  margin-bottom: 12px;
}
.content {
  height: 20vh;
  overflow-x: hidden;
}
/deep/.el-upload--picture-card {
  width: 400px;
  height: 270px;
}
/deep/.el-upload--picture-card i {
  margin-top: 128px;
}
</style>